package com.itheima.pinda.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.pinda.DTO.transportline.TransportLineDto;
import com.itheima.pinda.common.CustomIdGenerator;
import com.itheima.pinda.common.utils.Constant;
import com.itheima.pinda.entity.PdTransportLine;
import com.itheima.pinda.mapper.PdTransportLineMapper;
import com.itheima.pinda.mapper.PdTransportLineTypeMapper;
import com.itheima.pinda.service.PdTransportLineService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;

@Service
public class PdTransportLineServiceImpl implements PdTransportLineService {

    @Autowired
    private CustomIdGenerator idGenerator;
    @Autowired
    private PdTransportLineMapper transportLineMapper;

    @Override
    public TransportLineDto insert(TransportLineDto transportLineDto) {
        PdTransportLine transportLine = new PdTransportLine();
        BeanUtils.copyProperties(transportLineDto,transportLine);

        if (StringUtils.isEmpty(transportLine.getId())){
            // 新增
            transportLine.setId(idGenerator.nextId(transportLine) + "");
            transportLineMapper.insert(transportLine);
            transportLineDto.setId(transportLine.getId());
        }else {
            // 修改
            transportLineMapper.updateById(transportLine);
        }
        return transportLineDto;
    }

    @Override
    public TransportLineDto selectById(String id) {
        PdTransportLine transportLine = transportLineMapper.selectById(id);
        TransportLineDto transportLineDto = new TransportLineDto();
        BeanUtils.copyProperties(transportLine,transportLineDto);
        return transportLineDto;
    }

    @Override
    public IPage selectByPage(Integer pageNum, Integer pageSize, String lineNumber, String name, String transportLineTypeId) {
        Page<PdTransportLine> page = new Page<>(pageNum, pageSize);

        Page<PdTransportLine> selectPage = transportLineMapper.selectPage(page,
                new LambdaQueryWrapper<PdTransportLine>()
                        .eq(!StringUtils.isEmpty(lineNumber), PdTransportLine::getLineNumber, lineNumber)
                        .eq(!StringUtils.isEmpty(name), PdTransportLine::getName, name)
                        .eq(!StringUtils.isEmpty(transportLineTypeId), PdTransportLine::getTransportLineTypeId, transportLineTypeId));


        ArrayList<TransportLineDto> transportLineDtos = new ArrayList<>();
        List<PdTransportLine> records = selectPage.getRecords();
        for (PdTransportLine transportLine : records) {
            TransportLineDto transportLineDto = new TransportLineDto();
            BeanUtils.copyProperties(transportLine,transportLineDto);
            transportLineDtos.add(transportLineDto);
        }
        return new Page<TransportLineDto>().setRecords(transportLineDtos);
    }

    @Override
    public void deleteById(List<String> ids) {
        transportLineMapper.update(null,
                new LambdaUpdateWrapper<PdTransportLine>()
                        .set(PdTransportLine::getStatus, Constant.DATA_DISABLE_STATUS)
                        .in(PdTransportLine::getId,ids));
    }
}
